﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 数据管理类（PS:数据处理和更新UI显示写在一起）
/// </summary>
public class DataManager : MonoSingleton<DataManager>
{    
    public UserData mUserData = new UserData();
    public GameData mGameData = new GameData();
    
    /// <summary>
    /// 保存本地持久化数据
    /// </summary>
    public void SaveUserData()
    {        
        mUserData.UpdateForSave();
        CL.DataBase.saveData(mUserData);
    }
    /// <summary>
    /// 读取本地持久化数据
    /// </summary>
    public void ReadUserData()
    {
        CL.DataBase.readData(ref mUserData);
        mUserData.UpdateAfterRead();
    }
    /// <summary>
    /// Load时读取数据
    /// </summary>
    public void LoadData()
    {
        ReadUserData();
    }

    /// <summary>
    /// 重置本地持久化数据（清零）
    /// </summary>
    public void ResetUserData()
    {
        mUserData.ResetUserData();
        EventCenter.Broadcast(CL.EventType.UpdateSkinButtonImage, mUserData.selectSkin);
        SaveUserData();
        //由于最高分排行榜只有UIGameOverOpen时才会更新UI显示，重置数据也要直接更新
        BestScoreRank();
    }
    /// <summary>
    /// 重置游戏内数据（每次进游戏）
    /// </summary>
    public void ResetGameData()
    {
        mGameData.ResetGameData();
        EventCenter.Broadcast(CL.EventType.UpdateScoreOnUIGame);
        EventCenter.Broadcast(CL.EventType.UpdateDiamondOnUIGame);
        EventCenter.Broadcast(CL.EventType.InitPlayerControllerPropertyData, mUserData.selectSkin);
    }
    ///// <summary>
    ///// 重置玩家控制角色的属性数据（位置，大小，皮肤等）
    ///// </summary>
    //public void ResetPlayerControllerPropertyData()
    //{

    //}

    /// <summary>
    /// 购买皮肤减钻石
    /// </summary>
    public void BuyPlayerSkin(int indexSkin)
    {
        mUserData.diamondCount -= mUserData.skinUnlockDiamond[indexSkin];
        mUserData.skinUnlockState[indexSkin] = true;
        EventCenter.Broadcast(CL.EventType.UpdateDiamondCountUI);
        EventCenter.Broadcast(CL.EventType.UpdateSkinUnlockStateUI, indexSkin);
    }

    /// <summary>
    /// 选择皮肤
    /// </summary>
    public void SelectPlayerSkin(int indexSkin)
    {
        mUserData.selectSkin = indexSkin;
        EventCenter.Broadcast(CL.EventType.UpdateSkinButtonImage, mUserData.selectSkin);        
    }

    /// <summary>
    /// 玩家踩到一般平台加分
    /// </summary>
    public void AddScore(int addScore = 1)
    {
        mGameData.currentScore += addScore;
        EventCenter.Broadcast(CL.EventType.UpdateScoreOnUIGame);
    }
    /// <summary>
    /// 玩家吃到钻石
    /// </summary>
    /// <param name="addDiamond"></param>
    public void AddDiamond(int addDiamond = 1)
    {
        //只要是改变了UserData的数据，就要立即保存
        mUserData.diamondCount += addDiamond;
        SaveUserData();
        print("玩家总的钻石数量 = " + mUserData.diamondCount);
        //不要忘记游戏当前所得钻石数量也要改，以及更改UI显示
        mGameData.currentDiamond += addDiamond;
        EventCenter.Broadcast(CL.EventType.UpdateDiamondOnUIGame);
    }


    /// <summary>
    /// 算作数据的处理，所以写在DataManager
    /// </summary>
    public void Update()
    {
        if (GameManager.Instance.isGameStart && !GameManager.Instance.isGameOver)
        {
            DropTime();
        }
    }
    /// <summary>
    /// 平台掉落时间缩短
    /// </summary>
    private void DropTime()
    {
        if (mGameData.currentScore > mGameData.milestone)
        {
            mGameData.milestone *= 2;
            if (mGameData.DropTime <= mGameData.minDropTime)
            {
                mGameData.DropTime = mGameData.minDropTime;
            }
            else
            {
                mGameData.DropTime *= mGameData.dropTimeMultiple;
            }
        }
    }

    /// <summary>
    /// 最高分排行榜数据处理
    /// </summary>
    public void BestScoreRank()
    {
        mUserData.bestScore[3] = mGameData.currentScore;
        if (mGameData.currentScore > mUserData.bestScore[0])
        {
            EventCenter.Broadcast(CL.EventType.UpdateNewTrue);
        }
        else
        {
            EventCenter.Broadcast(CL.EventType.UpdateNewFalse);
        }
        mGameData.currentScore = 0;
        int n;
        for (int i = 0; i < mUserData.bestScore.Count + 1; i++)
        {
            for (int j = i + 1; j < mUserData.bestScore.Count; j++)
			{
                if (mUserData.bestScore[i] < mUserData.bestScore[j])
                {
                    n = mUserData.bestScore[i];
                    mUserData.bestScore[i] = mUserData.bestScore[j];
                    mUserData.bestScore[j] = n;
                }
			}            
        }
        SaveUserData();
        EventCenter.Broadcast(CL.EventType.UpdateUIRankUI);        
    }

    /// <summary>
    /// 游戏结算界面数据处理（其实就是更新UI显示）
    /// </summary>
    public void GameSettlement()
    {
        EventCenter.Broadcast(CL.EventType.UpdateUIGameOverUI);
    }

    /// <summary>
    /// 游戏是否静音数据处理
    /// </summary>
    public void IsSilent(bool isSilent)
    {
        mUserData.isSilent = isSilent;
        SaveUserData();
    }
}
